package com.fzrskj.cloud.free.user.utils;

import com.fzrskj.cloud.free.api.API;
import com.fzrskj.cloud.free.api.AlipayAPI;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.tool.CSessionTool;
import com.fzrskj.cloud.free.user.bean.UserCharge;
import com.fzrskj.cloud.free.user.bean.statistics.Statistics;
import com.fzrskj.cloud.free.user.dao.UserChargeDao;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import net.sf.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserChargeUtils extends CObject{
	/**
	 * 获取充值记录
	 * @param userName
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public List<UserCharge> getUserCharge(String userName, int page, int pageSize, String startTime, String endDate, String orderId){
		UserChargeDao dao =  new UserChargeDao();
		List<UserCharge> getUserCharge = dao.getUserCharge(userName, page, pageSize,startTime,endDate,orderId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return getUserCharge;
	}

	/**
	 * 添加充值记录
	 * @param userCharge
	 * @return
	 */
	public int addUserCharge(UserCharge userCharge){
		UserChargeDao dao =  new UserChargeDao();
		int i = dao.addUserCharge(userCharge);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return i;
	}

	/**
	 * 查询充值记录总数
	 * @param userName
	 * @param startTime
	 * @param endDate
	 * @param orderId
	 * @return
	 */
	public int UserChargePage(String userName,String startTime,String endDate,String orderId){
		UserChargeDao dao =  new UserChargeDao();
		int count = dao.UserChargePage(userName, startTime, endDate, orderId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return count;
	}

	public String OnlineRecharge(int rechargeType,String param) {
		API api = new API();
		String result = api.OnlineRecharge(rechargeType, param);
		JSONObject obj = JSONObject.fromObject(result);
		int code=obj.getInt("code");
		if(code<0){
			String message=obj.getString("message");
			int userId= CJsonTool.getIntValue(param, "uid", 0);
			UserLogUtils.addLog(this, userId, message);
		}
		obj=null;
		return result;
	}

	public int uptUsercharge(UserCharge uc){
		UserChargeDao dao =  new UserChargeDao();
		Map<String, Object> values = new HashMap<String, Object>();
		values.put("status", uc.getStatus());
		values.put("remark", uc.getRemark());
		Map<String, Object> cdt = new HashMap<String, Object>();
		cdt.put("orderId", uc.getOrderId());
		int ret = dao.modUserCharge(values, cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return ret;
	}

	public String notifyVerify(int typeId,String notifyId) {
		AlipayAPI alipayDao = new AlipayAPI();
		String result = "";
		switch (typeId) {
			case 1:result = alipayDao.alipayNotifyVerify(notifyId);break;
			case 2:break;
			default:
				JSONObject obj = new JSONObject();
				obj.put("code", "-1");
				obj.put("message", "无法识别的充值代码");
				result = obj.toString();
				break;
		}
		String code = alipayDao.getErrCode();
		this.setErrCode(code);
		alipayDao=null;
		return result;
	}

	/**
	 * 获取充值记录
	 * @param orderId
	 * @return
	 */
	public UserCharge getUserCharge(String orderId){
		UserChargeDao dao =  new UserChargeDao();
		UserCharge userCharge = dao.queryUserChargeByOederId(orderId);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return userCharge;
	}

	/**
	 * 手工处理订单
	 * @param orderId
	 * @param sid
	 * @return
	 */
	public String rechargeManual(String orderId, String sid){
		UserCharge UserCharge = new UserCharge();
		UserCharge = getUserCharge(orderId);
		String code1 = getErrCode();
		this.setErrCode(code1);
		if(!code1.equals(SysErr.CODE_SUCCESS)){
			dispose();
			JSONObject obj = new JSONObject();
			obj.put("code", "-1");
			obj.put("message", obj.getString("message").trim());
			return obj.toString();
		}
		String userName = UserCharge.getUserName();
		String voucherId = UserCharge.getOrderId();
		String bankCode = UserCharge.getBankCode();
		double amount = UserCharge.getAmount();
		String optName = CSessionTool.getAccountCloud(sid);
		String remark = String.format("管理员[%s]为用户[%s]手工处理充值订单，并给用户入款，充值订单号为[%s]", optName,userName, voucherId);
		FuncHandleUtils FuncHandleUtils = new FuncHandleUtils();
		String ret = FuncHandleUtils.FinanceAccount(userName,100,remark,voucherId,bankCode,"",amount,optName);
		return ret;
	}

	/**
	 * 统计充值订单总金额
	 * @param cdt
	 * @return
	 */
	public double queryChargeAmount(Map<String, Object> cdt){
		UserChargeDao dao =  new UserChargeDao();
		double amount = dao.queryChargeAmount(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return amount;
	}

	/**
	 * 统计充值(分类)
	 * @param cdt
	 * @return
	 */
	public List<Object[]> queryChargeSum(Map<String, Object> cdt){
		UserChargeDao dao =  new UserChargeDao();
		List<Object[]> amount = dao.queryChargecount(cdt);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return amount;
	}

	/**
	 * 按时间段统计充值金额
	 * @param cdt
	 * @param qryType H 小时 D 天 M 月 Y 年
	 * @return
	 * add wzj by 2018/12/12
	 */
	public List<Statistics> queryOrderCount(Map<String, Object> cdt, String qryType){
		UserChargeDao dao =  new UserChargeDao();
		List<Statistics> rets = dao.queryChargeCount(cdt, qryType);
		String code = dao.getErrCode();
		this.setErrCode(code);
		dao.dispose();
		return rets;
	}
}